﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;
using ClassLibrary.Entity;

public partial class Admin_UserControl_ucAddNewLoanPhone : System.Web.UI.UserControl
{
    TblPhoneBranDAO BrandDAO = new TblPhoneBranDAO(Global.CONNECTION_STRING);
    TblModelDAO modelDAO = new TblModelDAO(Global.CONNECTION_STRING);
    TblLoan2PhoneDAO loan2PhoneDAO = new TblLoan2PhoneDAO(Global.CONNECTION_STRING);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadBrand();
            loadModel();
        }
    }

    private void loadBrand()
    {
        try
        {
            DataTable dt = BrandDAO.GetAll();
            cbBrand.DataSource = dt;
            cbBrand.DataTextField = "BrandName";
            cbBrand.DataValueField = "IDBrand";
            cbBrand.DataBind();
            ListItem lstItem = new ListItem("- Select -", "");
            cbBrand.Items.Insert(0, lstItem);

        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }
    }

    private void loadModel()
    {
        try
        {
            DataTable dt = modelDAO.GetModel();
            cbModel.DataSource = dt;
            cbModel.DataTextField = "Model";
            cbModel.DataValueField = "Model";
            cbModel.DataBind();
            ListItem lstItem = new ListItem("- Select -", "");
            cbModel.Items.Insert(0, lstItem);

        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }
    }

    protected void cbBrand_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            DataTable dt = modelDAO.GetModelByIdBrand(Convert.ToInt32(cbBrand.SelectedValue));
            foreach (DataRow dr in dt.Rows)
            {
                cbModel.DataSource = dt;
                cbModel.DataTextField = "Model";
                cbModel.DataValueField = "Model";
                cbModel.DataBind();
            }
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        bool flag = false;
        try
        {
            TblLoan2Phone loan2phone = new TblLoan2Phone();


            loan2phone.LPhoneBrand = cbBrand.SelectedItem.Text;
            loan2phone.LPhoneModel = cbModel.SelectedItem.Text;
            loan2phone.LPIMENo = txt_IME.Text;
            loan2phone.LoanStatus = 0;
            loan2phone.LoanFee = Convert.ToDecimal(txt_loanfee.Text);
            loan2phone.Deposit = Convert.ToDecimal(txt_amount.Text);
            loan2phone.AccessoriresIncluded = txt_access.Text;
            loan2phone.Notes = txt_notes.Text;

            loan2phone.LPhoneID = "0";

            if (txt_IME.Text.Trim().Length == 15)
            {
                loan2PhoneDAO.InsUpd_L2Phone(loan2phone, 1);
            }
            else
            {
                ScriptManager.RegisterStartupScript(btnSave, btnSave.GetType(), "alert", "alert(' Phone IMEI No must be enough 15 characters');", true);
                txt_IME.Focus();
            }
            flag = true;
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }
        if (flag)
        {
            Response.Redirect("ListLoanPhone.aspx");
        }
    }
   
}
